<!DOCTYPE html>
<html>
	{include file="public/_header" /}
	<body>
		<div class="x-nav">
			<span class="layui-breadcrumb">
				<a href="">首页</a>
				<a>
					<cite>友情链接</cite></a>
			</span>
			<a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" onclick="location.reload()"
			 title="刷新">
				<i class="layui-icon layui-icon-refresh" style="line-height:30px"></i></a>
		</div>
		<div class="layui-fluid">
			<div class="layui-row layui-col-space15">
				<div class="layui-col-md12">
					<div class="layui-card">
						<div class="layui-card-body ">
							<div class="layui-tab layui-tab-card" lay-filter="test">
							  <ul class="layui-tab-title">
								{foreach $group as $key=>$vo}
									<li lay-id="{$vo.gid}" {eq name="$key" value="0"}class="layui-this"{/eq}>{$vo.gname}</li>
								{/foreach}
							  </ul>
							  <div class="layui-tab-content">
							    <div class="layui-tab-item layui-show">
							      <table class="layui-hide" id="test" lay-filter="test"></table>
							    </div>
							  </div>
							</div>
							
						</div>
					</div>
				</div>
			</div>
		</div>

		<script type="text/html" id="toolbarDemo">
			<div class="layui-btn-container">
					<button class="layui-btn layui-btn-sm" lay-event="getCheckData">
						 <i class="layui-icon">&#xe608;</i>{:lang('add')}
					</button>
				</div>
		</script>

		<script type="text/html" id="barDemo">
			<a class="layui-btn layui-btn-xs" lay-event="edit">{:lang('edit')}</a>
		  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">{:lang('delete')}</a>
		</script>

		<script>
			function check_validate1(value){
			    //定义正则表达式部分
			    var reg = /^\d+$/;
			    if( value.match( reg )){
			        return true;
			    }
			    return false;
			}
			var url = "__STATIC__/admin/"
			$(function(){
				var group_id = 1;
				layui.use(['table','element'], function() {
					var table = layui.table;
					var form = layui.form;
					var element = layui.element;
					
					var tableIns = table.render({
						elem: '#test',
						url: "{:url('admin/Config/config_list')}",
						toolbar: '#toolbarDemo',
						title: '用户数据表',
						where:{
							group_id:group_id
						},
						cols: [
							[ {
								field: 'sort',
								title: "{:lang('sort')}",
								width: 100,
								edit: true,
								sort: true
							},
							{
								field: 'title',
								title: "{:lang('name')}"
							}, {
								field: 'name',
								title: "{:lang('title')}"
							},{
								field: 'type',
								title: "{:lang('nametype')}",
								sort: true
							},{
								fixed: 'right',
								title: "{:lang('action')}",
								toolbar: '#barDemo',
								width: 150
							}]
						],
						id: 'testReload',
						page: true
					});
					element.on('tab(test)', function(data){
					  group_id = $(this).attr("lay-id")
					  tableIns.reload({
						  where:{
						  	group_id:group_id
						  }
					  })
					});
					//头工具栏事件
					table.on('toolbar(test)', function(obj) {
						var checkStatus = table.checkStatus(obj.config.id);
						switch (obj.event) {
							case 'getCheckData':
								var index = layer.open({
									title: "{:lang('add')}",
									type: 2,
									area: ['800px', '680px'],
									content: "{:url('admin/Config/addconfig')}",
									success:function(){
										top.layerFrameConfig.iframeB = {
											info:tableIns,
											index:index
										}
									}
								});
								break;
							case 'getCheckLength':
								var data = checkStatus.data;
								
								if(data.length<1){
									layer.msg('您最少选择一个选项进行操作');
									return false;
								}
								var ids = [];
								data.forEach(function(item,index){
									ids.push(item.id)
								})
								layer.confirm('确认要删除这些内容？', function(){
								  $.post("{:url('admin/Link/link_all_dele')}",{ids:ids},function(res){
								  	if(res.status == 1){
								  		layer.msg('删除成功', {
								  			icon: 1,
								  			time: 300
								  		}, function() {
								  			tableIns.reload()
								  		});
								  	}else{
								  		layer.msg(res.message)
								  	}
								  })
								});
								
								break;
							case 'type':
								layer.open({
									title: "{:lang('typelist')}",
									type: 2,
									area: ['900px', '600px'],
									content: "{:url('admin/Link/typelists')}",
									success: function(layero, index) {
										top.layerFrameConfig.iframeB = {
											layer_index: index
										}
									}
								});
								break;
						};
					});
				
					//监听行工具事件
					table.on('tool(test)', function(obj) {
						var data = obj.data;
						//console.log(obj)
						if (obj.event === 'del') {
							layer.confirm('真的删除行么', function(index) {
								$.post("{:url('admin/Link/linkdele')}",data,function(res){
									if(res.status == 1){
										layer.msg('删除成功', {
											icon: 1,
											time: 300
										}, function() {
											layer.close(index);
											tableIns.reload()
										});
									}else{
										layer.msg(res.message)
									}
								})
								
							});
						} else if (obj.event === 'edit') {
							layer.open({
								title: '编辑',
								type: 2,
								area: ['800px', '680px'],
								content: "{:url('admin/Link/linkedit')}?id="+data.id,
								success:function(){
									top.layerFrameConfig.iframeB = {
										info:tableIns
									}
								}
							});
						}
					});
					//监听单元格编辑
					table.on('edit(test)', function(obj) {
						var value = obj.value, //得到修改后的值
							data = obj.data, //得到所在行所有键值
							field = obj.field; //得到字段
							if(!check_validate1(value)){
								layer.msg('你必须输入数字');
								tableIns.reload()
							}else{
								sort(obj,tableIns)
							}
							
						
					});
					table.on('rowDouble(test)', function(obj){
					  console.log(obj)
					});
					form.on('switch(is_check)', function(obj){
						// console.log(this.value,this.name,obj.elem.checked,obj.othis)
						var data = {
							id:this.value,
							status:obj.elem.checked?1:0
						}
						$.post("{:url('admin/Link/link_check')}",data,function(res){
							if(res.status == 1){
								layer.msg('设置成功');
							}else{
								layer.msg(res.message);
								tableIns.reload()
							}
						})
					});      
					  
				});
				
			})
			function sort(obj,tableIns){
				$.post("{:url('admin/Link/linksort')}",obj.data,function(res){
					if(res.status == 1){
						tableIns.reload()
					}
				})
			}
		</script>
	</body>
</html>
